/**
 * 
 */
package Consult.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;


import Consult.dao.model.UserType;
import Consult.model.User;
import Util.DbCUtil;
import Util.StringUtil;
import javax.swing.JComboBox;

/**
 * @author zhaoyana
 *
 */
public class WelecomFrame extends JFrame {

	private JPanel contentPane;
	private JTextField userNametxt;
	private JPasswordField passwordtxt;
	private JComboBox Admin;
private DbCUtil dbUtil=new DbCUtil();
private Com.userDao userDao =new Com.userDao();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					WelecomFrame frame = new WelecomFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public WelecomFrame() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(WelecomFrame.class.getResource("/images/图书馆空间借用申请.png")));
		setTitle("图书馆系统登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 924, 671);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.inactiveCaption);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("图书管理系统");
		lblNewLabel.setIcon(new ImageIcon(WelecomFrame.class.getResource("/images/图书馆s.png")));
		lblNewLabel.setFont(new Font("Dialog", Font.PLAIN, 39));
		
		JLabel lblNewLabel_2 = new JLabel("密码");
		lblNewLabel_2.setIcon(new ImageIcon(WelecomFrame.class.getResource("/images/密码1.png")));
		lblNewLabel_2.setFont(new Font("Dialog", Font.PLAIN, 31));
		
		JLabel lblNewLabel_1 = new JLabel("账号");
		lblNewLabel_1.setIcon(new ImageIcon(WelecomFrame.class.getResource("/images/账号1.png")));
		lblNewLabel_1.setFont(new Font("Dialog", Font.PLAIN, 31));
		
		userNametxt = new JTextField();
		userNametxt.setFont(new Font("Dialog", Font.PLAIN, 28));
		userNametxt.setColumns(10);
		
		passwordtxt = new JPasswordField();
		passwordtxt.setFont(new Font("Dialog", Font.PLAIN, 28));
		
		JButton btnNewButton = new JButton("注册");
		btnNewButton.setBackground(UIManager.getColor("ToolBar.highlight"));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ReActionPerformed(e);
			}
		});
		btnNewButton.setFont(new Font("Microsoft JhengHei", Font.BOLD, 28));
		
		JButton btnNewButton_1 = new JButton("登录");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginActionPerformed(e);
			}
		});
		btnNewButton_1.setBackground(SystemColor.info);
		btnNewButton_1.setFont(new Font("Microsoft YaHei UI Light", Font.BOLD, 28));
		
		JLabel lblNewLabel_3 = new JLabel("");
		
		JLabel lblNewLabel_4 = new JLabel("用户类型");
		lblNewLabel_4.setFont(new Font("Dialog", Font.PLAIN, 22));
		lblNewLabel_4.setIcon(new ImageIcon(WelecomFrame.class.getResource("/images/头像.png")));
		
	 Admin = new JComboBox();
	 Admin.setToolTipText("halo");
	 Admin.setModel(new DefaultComboBoxModel(new UserType[] {UserType.ADMIN,UserType.STUDENT}));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(291)
					.addComponent(lblNewLabel_4)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_3)
						.addComponent(Admin, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(244, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(122)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblNewLabel_2)
							.addGap(18)
							.addComponent(passwordtxt, GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblNewLabel_1)
							.addGap(18)
							.addComponent(userNametxt, 483, 483, 483)))
					.addGap(163))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(192)
					.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 250, GroupLayout.PREFERRED_SIZE)
					.addGap(112)
					.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 232, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(110, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(194, Short.MAX_VALUE)
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 554, GroupLayout.PREFERRED_SIZE)
					.addGap(148))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(7)
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
							.addComponent(lblNewLabel_3)
							.addGap(43))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel_4)
								.addComponent(Admin, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(userNametxt, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE))
					.addGap(38)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_2)
						.addComponent(passwordtxt, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton_1, GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE))
					.addGap(111))
		);
		contentPane.setLayout(gl_contentPane);
		this.setLocationRelativeTo(null);//设置页面居中
	}

	/**
	 * @param e
	 */
	protected void ReActionPerformed(ActionEvent e) {
		dispose();//销毁当前窗口
		new RegFrm().setVisible(true);//显示窗体
		
	}

	/**
	 * @param e
	 * 登录事件处理
	 */
	protected void loginActionPerformed(ActionEvent event) {
		String userName=this.userNametxt.getText();
		String password=new String(this.passwordtxt.getPassword());// TODO Auto-generated method stub
		if(StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null,"用户名不能为空");
			return;
		}
		if(StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null,"密码不能为空");
			return;
		}
		User user=new User(userName,password);
	Connection con=null;
	try {
		con=dbUtil.getCon();
		User currentUser=userDao.login(con, user);
	if (currentUser != null) {
		JOptionPane.showMessageDialog(null,"欢迎进入图书馆！");
		dispose();
		new MainFrame().setVisible(true);
	}else {
		JOptionPane.showMessageDialog(null,"很抱歉，密码或账号有误");
	}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		try {
			dbUtil.closeCon(con);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	}
}
